var inputData = [];
var allData = [];
var titleData=[];
init2(inputData);
function getOptions2() {
    var option = [];
    allData.forEach(function(data,index){
        var series = [];
        var xAxisData =[];
        var seriesData=[];
        var seriesData1=[];
        var seriesData2=[];
        data.cltvList.forEach(function (data,index) {
            // xAxisData[index]=data.NAME;
            xAxisData[index]=data.distrcit_name;
            var ojb={
                // name: data.NAME,
                name: data.distrcit_name,
                // value:data.PRICE.toFixed(0)
                value:data.price1
            };
            seriesData[index] = ojb;
            var ojb1={
                // name: data.NAME,
                name: data.distrcit_name,
                // value:data.PRICE.toFixed(0)
                value:data.price2
            };
            seriesData1[index] = ojb1;
            var ojb2={
                // name: data.NAME,
                name: data.distrcit_name,
                // value:data.PRICE.toFixed(0)
                value:data.dvalue
            };
            seriesData2[index] = ojb2;
        });
        var xAxis = {
            data:xAxisData
        };
        series[0] ={
            data:seriesData
        };
        series[1] ={
            data:seriesData1
        };
        series[2] ={
            data:seriesData2
        };
        var dataMap ={
            xAxis:xAxis,
            series:series
        };
        option[index] = dataMap;
    });
    return option;
}
function getOptionsN() {
    var option = [];
    var dataList = [
        {build:[
            {
                distrcit_name: "楼盘1",
                dvalue: 8757,
                price1: 38735,
                price2: 42322},
            {
                distrcit_name: "楼盘1",
                dvalue: -757,
                price1: 42337,
                price2: 28767},
            {
                distrcit_name: "楼盘1",
                dvalue: 4757,
                price1: 34543,
                price2: 67534},
            {
                distrcit_name: "楼盘1",
                dvalue: -137,
                price1: 32348,
                price2: 32142},
            {
                distrcit_name: "楼盘1",
                dvalue: -3735,
                price1: 37834,
                price2: 32437},
        ],
            year:2011},
        {build:[
            {
                distrcit_name: "楼盘2",
                dvalue: -7375,
                price1: 21374,
                price2: 23738},
            {
                distrcit_name: "楼盘2",
                dvalue: -343,
                price1: 37358,
                price2: 32737},
            {
                distrcit_name: "楼盘2",
                dvalue: 4857,
                price1: 43573,
                price2: 37435},
            {
                distrcit_name: "楼盘2",
                dvalue: 3735,
                price1: 32438,
                price2: 37354},
            {
                distrcit_name: "楼盘2",
                dvalue: 7367,
                price1: 34753,
                price2: 37832},
        ],
            year:2012},
        {build:[
            {
                distrcit_name: "楼盘3",
                dvalue: -3573,
                price1: 37837,
                price2: 42377},
            {
                distrcit_name: "楼盘3",
                dvalue: -7657,
                price1: 21378,
                price2: 38372},
            {
                distrcit_name: "楼盘3",
                dvalue: -11354,
                price1: 53423,
                price2: 34578},
            {
                distrcit_name: "楼盘3",
                dvalue: -37583,
                price1: 67352,
                price2: 37358},
            {
                distrcit_name: "楼盘3",
                dvalue: -3237,
                price1: 32378,
                price2: 23783},
        ],
            year:2013},
        {build:[
            {
                distrcit_name: "楼盘4",
                dvalue: -3873,
                price1: 54538,
                price2: 65437},
            {
                distrcit_name: "楼盘4",
                dvalue: -3543,
                price1: 38732,
                price2: 45375},
            {
                distrcit_name: "楼盘4",
                dvalue: -3873,
                price1: 32438,
                price2: 37832},
            {
                distrcit_name: "楼盘4",
                dvalue: -4537,
                price1: 34238,
                price2: 54154},
            {
                distrcit_name: "楼盘4",
                dvalue: -1377,
                price1: 37838,
                price2: 38735},
        ],
            year:2014},
        {build:[
            {
                distrcit_name: "楼盘5",
                dvalue: -35757,
                price1: 34534,
                price2: 76724},
            {
                distrcit_name: "楼盘5",
                dvalue: -877,
                price1: 25378,
                price2: 35438},
            {
                distrcit_name: "楼盘5",
                dvalue: -3437,
                price1: 38732,
                price2: 30137},
            {
                distrcit_name: "楼盘5",
                dvalue: -5757,
                price1: 43558,
                price2: 37382},
            {
                distrcit_name: "楼盘5",
                dvalue: -3735,
                price1: 21380,
                price2: 30137},
        ],
            year:2015},
        {build:[
            {
                distrcit_name: "楼盘6",
                dvalue: -8757,
                price1: 21380,
                price2: 30137}
        ],
            year:2016},
        {build:[
            {
                distrcit_name: "楼盘7",
                dvalue: -8757,
                price1: 21380,
                price2: 30137},
            {
                distrcit_name: "楼盘7",
                dvalue: -8757,
                price1: 21380,
                price2: 30137},
            {
                distrcit_name: "楼盘7",
                dvalue: -8757,
                price1: 21380,
                price2: 30137},
            {
                distrcit_name: "楼盘7",
                dvalue: -8757,
                price1: 21380,
                price2: 30137},
            {
                distrcit_name: "楼盘7",
                dvalue: -8757,
                price1: 21380,
                price2: 30137},
        ],
            year:2017},
        {build:[
            {
                distrcit_name: "楼盘8",
                dvalue: -8757,
                price1: 21380,
                price2: 30137},
            {
                distrcit_name: "楼盘8",
                dvalue: 18757,
                price1: 31380,
                price2: 12137},
            {
                distrcit_name: "楼盘8",
                dvalue: 8757,
                price1: 45304,
                price2: 78920},
            {
                distrcit_name: "楼盘8",
                dvalue: -1657,
                price1: 8643,
                price2: 13870},
            {
                distrcit_name: "楼盘8",
                dvalue: -18757,
                price1: 58376,
                price2: 31378},
        ],
            year:2018},
        {build:[
            {
                distrcit_name: "楼盘9",
                dvalue: 38757,
                price1: 57321,
                price2: 48372},
            {
                distrcit_name: "楼盘9",
                dvalue: 28757,
                price1: 24838,
                price2: 73538},
            {
                distrcit_name: "楼盘9",
                dvalue: 18757,
                price1: 54384,
                price2: 38724},
            {
                distrcit_name: "楼盘9",
                dvalue: -8757,
                price1: 45876,
                price2: 30137},
            {
                distrcit_name: "楼盘9",
                dvalue: 8757,
                price1: 65024,
                price2: 30137},
        ],
            year:2019},
    ]
    dataList.forEach(function(data,index){
        var series = [];
        var xAxisData =[];
        var seriesData=[];
        var seriesData1=[];
        var seriesData2=[];
        data.build.forEach(function (data,index) {
            // xAxisData[index]=data.NAME;
            xAxisData[index]=data.distrcit_name;
            var ojb={
                // name: data.NAME,
                name: data.distrcit_name,
                // value:data.PRICE.toFixed(0)
                value:data.price1
            };
            seriesData[index] = ojb;
            var ojb1={
                // name: data.NAME,
                name: data.distrcit_name,
                // value:data.PRICE.toFixed(0)
                value:data.price2
            };
            seriesData1[index] = ojb1;
            var ojb2={
                // name: data.NAME,
                name: data.distrcit_name,
                // value:data.PRICE.toFixed(0)
                value:data.dvalue
            };
            seriesData2[index] = ojb2;
        });
        var xAxis = {
            data:xAxisData
        };
        series[0] ={
            data:seriesData
        };
        series[1] ={
            data:seriesData1
        };
        series[2] ={
            data:seriesData2
        };
        var dataMap ={
            xAxis:xAxis,
            series:series
        };
        option[index] = dataMap;
    });
    return option;
}
function init2(data){
    $.ajax({
        type: "POST",
        url: "/bankingRegulatory/initData1",
        data:{city:'广州市'},
        // url: "/bankingRegulatory/priceVariance",
        // data:{city:410100},
        success: function (msg){
            if(msg.status=="false"){
                return
            }else{
                allData=msg.resultList;
                doCharts2(getOptions2());
                console.log(allData)
            }
        }
    });
}
function doCharts2(option){
    allData.forEach(function(data,index){
        titleData[index] = data.year;
    });
    var option5 = {
        baseOption: {
            timeline: {
                // y: 0,
                axisType: 'category',
                // realtime: false,
                // loop: false,
                autoPlay: false,
                currentIndex: titleData.length-1,
                playInterval: 1000,
                // controlStyle: {
                //     position: 'left'
                // },
                data: titleData,
                label: {
                    normal: {
                        color: '#ADADAD',
                    },
                },
            },
            title: {
                subtext: '广州市各行政区一手房，二手房价格统计表',
                x: 'center'
            },
            tooltip: {
            },
            calculable : true,
            grid: {
                top: 80,
                bottom: 100,
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: '',
                        label: {
                            show: false,
                            formatter: function (params) {
                                return params.value.replace('\n', '');
                            }
                        }
                    }
                }
            },
            xAxis: [
                {
                    'type':'category',
                    'axisLabel':{'interval':0},
                    'data':[],
                    splitLine: {show: false},
                    axisLine:{
                        lineStyle:{
                            color:'#ADADAD',
                        }
                    },
                    triggerEvent: true
                },

            ],
            yAxis: [
                {
                    type: 'value',
                    name: '金额/每平方米',
                    axisLine:{
                        lineStyle:{
                            color:'#ADADAD',
                        }
                    },
                }
            ],
            series: [
                {name: '一手房价格', type: 'bar'},
                {name: '二手房价格', type: 'bar'},
                {name: '价差', type: 'bar'},
            ]
        },
        options: option
    };
    var myChart = echarts.init($('#myChart5').get(0));
    myChart.setOption(option5);
    myChart.on('click',function (params) {
        if (params.componentType === 'xAxis'){
            doCharts2(getOptionsN());
        }
    });
    window.onresize = function () {
        myChart.resize()
    };
}
//搜索
$('#searchData').click(function(){
    var citys = $('#city').val(),
        inputData = {
            city:citys,
        };
    init2(inputData)
});
